﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ChisMethodsHomework12._10._11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void solveLayer(double[] u, int index, int n, double h, double t)
        {
            double [] alpha = new double [n];
            double [] beta = new double [n];
            alpha[0] = 2.0 * t / (h * h);
            beta[0] = t * u[(index - 1) * (n + 1) + 1];
            for (int i = 1; i < n; i++)
            {
                alpha[i] = 1.0 / (h * h * (1.0 / t - 2.0 * alpha [i - 1] / (h * h)));
                beta[i] = (u[(index - 1) * (n + 1) + i] * (1.0 / t + 2.0 / (h * h)) + beta[i - 1] / (h * h)) / (1.0 / t - 2.0 * alpha[i - 1] / (h * h));
            }
            u[index * (n + 1) + n] = beta[n - 1] / (1 - alpha[n - 1]);
            for (int i = n - 2; i > -1; i--)
                u[index * (n + 1) + i] = alpha[i] * u[index * (n + 1) + i + 1] + beta[i];
            alpha = null;
            beta = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Число узлов
            int n = -1, m = -1;
            // Массив для хранения значений функции
            double[] u = null;
            // Причина выхода
            string cause;
            // Переменные для критерия останова
            double eps = -1, min_eps = -1.0;

            n = Convert.ToInt32(textBox1.Text);
            m = Convert.ToInt32(textBox2.Text);
            u = new double[(n + 1) * (m + 1)];
            double h = 1.0 / n;
            double t = 1.0 / m;
            for (int i = 0; i < m + 1; i++)
            {
                u[i * n] = Math.Sin(i * t) * Math.Sin(i * t) + Math.Cos(i * t);
                u[(i + 1) * n] = 0;
            }
            for (int i = 0; i < n + 1; i++ )
            {
                u[i] = 0;
            }

            for (int i = 1; i < m + 1; i++)
            {
                solveLayer(u, i, n, h, t);
            }
            label1.Text = "Complete!";
        }
    }
}
